package com.hangxintong.controller;


import com.hangxintong.common.StatusCode;
import com.hangxintong.common.base.dto.MessageDTO;
import com.hangxintong.common.base.entity.LmUser;
import com.hangxintong.common.base.entity.UserRepository;
import com.hangxintong.common.exception.SystemErrorCodeMark;
import com.hangxintong.common.exception.SystemException;
import com.hangxintong.common.utils.Result;
import com.hangxintong.jpa.InformationRepository;
import com.hangxintong.jpa.LmInformation;
import com.hangxintong.service.InformationService;
import com.hangxintong.service.MsgQueueService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


/**
 * @BelongsProject: coalition
 * @BelongsPackage: com.industory.coalition.controller
 * @Author: Mr.zhang
 * @CreateTime: 2020-01-08 11:39
 * @Description: 工作经验
 */
@SuppressWarnings("checkstyle:Indentation")
@Slf4j
@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping("/webStation")
@Api(tags = "消息管理站内信")
public class InformationController {
    @Autowired
    InformationRepository informationRepository;
    @Autowired
    UserRepository userRepository;

    @Autowired
    InformationService informationService;


    @ApiOperation(value = "添加/编辑（id）消息", notes = "添加消息", httpMethod = "POST")
    @PostMapping(value="/saveMessage")
    public Result add(HttpServletRequest request,
                      @RequestBody @Validated MessageDTO messageDTO
                      ) {
        try {
            return informationService.add(request, messageDTO);
        } catch (Exception e) {
            log.info(SystemErrorCodeMark.Code.DATA_NOT_EXIST+":"+e.getMessage());
            throw new SystemException(SystemErrorCodeMark.Code.DATA_NOT_EXIST,e.getMessage());
        }
    }

    @ApiOperation(value = "删除消息", notes = "删除消息", response = ResponseEntity.class, httpMethod = "DELETE")
    @DeleteMapping(value = "/{id}")
    public Result del(HttpServletRequest request,
                      @PathVariable(value = "id") String id) {

        Result del = informationService.del(request, id);
        return del;
    }

    @ApiOperation(value = "未读消息变已读")
    @PutMapping(value = "/{id}")
    public Result updateInfoStatus(HttpServletRequest request,
                                   @PathVariable(value = "id") String id) {
        try {

            Result del = informationService.updateInfoStatus(request, id);
            return del;
        }catch(Exception e){
            throw new SystemException(SystemErrorCodeMark.Code.DATA_NOT_EXIST,e.getMessage());
        }

    }

    @ApiOperation(value = "消息详情")
    @GetMapping(value = "/info/{id}")
    public Result info(HttpServletRequest request,
                                   @PathVariable(value = "id") String id) {
        Result del = informationService.findInfo(id);
        return del;
    }
    /**
    * @Description
    * @Author  Bz.Mr
    * @Date   2020/12/14 11:04
    *
    */
    @ApiOperation(value = "消息列表", notes = "消息列表", response = ResponseEntity.class, httpMethod = "GET")
    @GetMapping("/list")
    public Result list(HttpServletRequest request,
                       @RequestParam(value = "search", defaultValue = "", required = false) String search,
                       @RequestParam(value = "status", defaultValue = "", required = false) Integer status,
                       @RequestParam(value = "cate", defaultValue = "", required = false) Integer cate,
                       @RequestParam(value = "page", defaultValue = "1", required = false) Integer page,
                       @RequestParam(value = "size", defaultValue = "15", required = false) Integer size) {
        LmUser user = (LmUser) request.getAttribute("user");
        Page<LmInformation> lists = informationService.pagelist(status,cate,page,size,user,search);

        return new Result(true, StatusCode.OK, "ok", lists);
    }
    /**
    * @Description
    * @Author  Bz.Mr
    * @Date   2020/12/14 11:26
    * 查询未读消息
    */
    @ApiOperation(value = "查询收件人是否有未读消息", notes = "查询收件人是否有未读消息", response = ResponseEntity.class, httpMethod = "GET")
    @GetMapping("/listCount")
    public Result listCount(HttpServletRequest request,
                       @RequestParam(value = "cate", defaultValue = "", required = false) Integer status) {
        LmUser user = (LmUser) request.getAttribute("user");
        List<LmInformation> list = informationRepository.findBySjrIdAndStatus(user.getId().intValue(), status);
        return new Result(true, StatusCode.OK, "ok", list);
    }
    /**
     * @Description
     * @Author  Bz.Mr
     * @Date   2020/12/14 11:26
     * 页面详情中显示已恢复的数据
     */
    @ApiOperation(value = "页面详情中显示已恢复的数据", notes = "页面详情中显示已恢复的数据", response = ResponseEntity.class, httpMethod = "GET")
    @GetMapping("/listReplyMessage/{id}")
    public Result listReplyMessage(HttpServletRequest request,
                                   @PathVariable(value="id") String id) {
        LmUser user = (LmUser) request.getAttribute("user");
        List<LmInformation> list = informationService.listReplyMessage(user.getId().intValue(), id);
        return new Result(true, StatusCode.OK, "ok", list);
    }

    /**
     * @Description
     * @Author  Bz.Mr
     * @Date   2020/12/14 11:26
     * 协会下查看某条信息回复的记录
     */
    @ApiOperation(value = "协会查看回复的消息列表", notes = "协会查看消息下回复的消息列表", response = ResponseEntity.class, httpMethod = "GET")
    @GetMapping("/listMessages/{id}")
    public Result listMessages(HttpServletRequest request,
                                   @PathVariable(value="id") String id,
                               @RequestParam(value="page",defaultValue = "1") Integer page,
                               @RequestParam(value="size",defaultValue = "10") Integer size
                               ) {
        LmUser user = (LmUser) request.getAttribute("user");
        Sort sort = Sort.by(Sort.Direction.DESC, "id");
        Page<LmInformation> list = informationService.listMessages(id, PageRequest.of(page - 1, size, sort));
        return Result.ok(list);
    }
}
